
if exists (select 1 from sysobjects where name = 'set_prsabteilung' and type = 'P')
begin
   drop procedure set_prsabteilung
   print 'Procedure: set_prsabteilung deleted ...'
end
go
create procedure set_prsabteilung(
  @abteilungid      int   = 1,
  @prsid            int   = 1,
  @von              char(8) = '20070501',
  @bis              char(8) = '20070501',
  @prozent          int     = 0
)
as
begin
  set nocount on
--   if exists (select 1 from personalabteilung where abteilungid = @abteilungid and prsid = @prsid)
--   begin
--      update personalabteilung
--         set bis = @von
--       where abteilungid = @abteilungid 
--         and prsid = @prsid
--         and bis = (select max(pa.bis) from personalabteilung pa where pa.abteilungid = @abteilungid and pa.prsid = @prsid)
--   end

   insert into PersonalAbteilung (AbteilungID, PrsId, Von, Bis, Prozent, Status) values (@abteilungid, @prsid, @von, @bis, @prozent, 1)
end
go
print 'Procedure: set_prsabteilung done ...'
go
grant exec on set_prsabteilung to prsadmins with grant option
go
grant exec on set_prsabteilung to prsusers
go

